package com.monitor.aop;

import com.monitor.constant.URIPatternConstant;
import com.monitor.model.URLPattenEntity;
import com.monitor.service.URLPatternService;
import com.monitor.util.SimpleCache;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Aspect
@Slf4j
public class URLPatternAop {
    @Autowired
    private URLPatternService urlPatternService;

    @Pointcut(value = "execution(* com.monitor.service.URLPatternService.save*(..))")
    public void pointcutSave() {

    }

    @Pointcut(value = "execution(* com.monitor.service.URLPatternService.update*(..))")
    public void pointcutUpdate() {

    }

    @Pointcut(value = "execution(* com.monitor.service.URLPatternService.delete*(..))")
    public void pointcutDelete() {

    }

    @Pointcut(value = "execution(* com.monitor.service.URLPatternService.remove*(..))")
    public void pointcutRemove() {

    }

    @AfterReturning(value = "pointcutSave()||pointcutDelete()||pointcutRemove()||pointcutUpdate()")
    public void afterOperation(JoinPoint point) {
        List<URLPattenEntity> urlPatterns = urlPatternService.list();
        Set<String> uriPatterns = urlPatterns.stream().map(URLPattenEntity::getUriPatten).collect(Collectors.toSet());
        SimpleCache.getSimpleCache().put(URIPatternConstant.URI_PATTERN, uriPatterns);
    }
}
